﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VoiceManager : MonoBehaviour
{
    public enum VoiceType
    {
        Bg,
        Button,
        Effect
    }

    /// <summary>
    /// 相对于Reources的路径
    /// </summary>
    [SerializeField]
    private string voicePath="Voice/";

    [Autowrite]
    private AudioSource bgVoice;

    [Autowrite]
    private AudioSource buttonVoice;

    [Autowrite]
    private AudioSource effectVoice;

    public static VoiceManager Instance;


    public void Awake()
    {
        AutoInjectComponent.AutoInject(transform, this);
        if (Instance == null)
        {
            Instance = this;
        }
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    /// <summary>
    /// 播放声音
    /// </summary>
    public void PlayVoice(string voiceName,VoiceType type)
    {
        AudioClip clip = Resources.Load<AudioClip>(voicePath + voiceName);
        switch (type)
        {
            case VoiceType.Bg:
                bgVoice.clip = clip;
                bgVoice.loop = true;
                bgVoice.Play();
                break;
            case VoiceType.Button:
                buttonVoice.clip = clip;
                buttonVoice.loop = false;
                buttonVoice.Play();
                break;
            case VoiceType.Effect:
                effectVoice.clip = clip;
                effectVoice.loop = false;
                effectVoice.Play();
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 静音设置
    /// </summary>
    /// <param name="type"></param>
    /// <param name="mute"></param>
    public void MuteVoice(VoiceType type, bool mute)
    {
        switch (type)
        {
            case VoiceType.Bg:
                bgVoice.mute = mute;
                break;
            case VoiceType.Button:
                buttonVoice.mute = mute;
                break;
            case VoiceType.Effect:
                effectVoice.mute = mute;
                break;
            default:
                break;
        }
    }
}
